package com.wxit.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wxit.eduservice.entity.EduSubject;
import com.wxit.eduservice.entity.excel.SubjectData;
import com.wxit.eduservice.entity.subject.OneSubject;
import com.wxit.eduservice.entity.subject.TwoSubject;
import com.wxit.eduservice.listener.SubjectExcelListener;
import com.wxit.eduservice.mapper.EduSubjectMapper;
import com.wxit.eduservice.service.EduSubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author 吴杰
 * @since 2021-11-06
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    /**
     * 添加课程分类
     * @param file
     */
    @Override
    public void saveSubject(MultipartFile file,EduSubjectService eduSubjectService) {

        try {
            InputStream in = file.getInputStream();
            EasyExcel.read(in, SubjectData.class,new SubjectExcelListener(eduSubjectService)).sheet().doRead();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 课程分类 树形
     * @return
     */
    @Override
    public List<OneSubject> getAllOneAndTwoSubject() {
        //分析

        //查询所有一级分类  parent_id = 0
        QueryWrapper<EduSubject> wrapperOne = new QueryWrapper<>();
        wrapperOne.eq("parent_id","0");

        List<EduSubject> oneSubjectList = baseMapper.selectList(wrapperOne);
        //查询所有的二级分类

        QueryWrapper<EduSubject> wrapperTwo = new QueryWrapper<>();
        wrapperTwo.ne("parent_id","0");

        List<EduSubject> twoSubjectList = baseMapper.selectList(wrapperTwo);

        //封装一级分类
        //查询出来所有的一级分类，list集合遍历，得到每一个一级分类的对象，获取每一个一级分类的对象值，封装到要求的list集合里面List<OneSubject> finalSubjectList

        //创建list集合，用于存储最终封装数据
        List<OneSubject> finalSubjectList = new ArrayList<>();

        for (int i = 0; i < oneSubjectList.size(); i++) {
            EduSubject eduSubject = oneSubjectList.get(i);

            //把eduSubject里的值取出来，放到OneSubject对象里面
            OneSubject oneSubject = new OneSubject();
//            oneSubject.setId(eduSubject.getId());
//            oneSubject.setTitle(eduSubject.getTitle());
            BeanUtils.copyProperties(eduSubject,oneSubject);
            finalSubjectList.add(oneSubject);

            //
            List<TwoSubject> twoFinalSubjectList = new ArrayList<>();
            for (int m = 0; m < twoSubjectList.size(); m++) {
                //获取每一个二级分类
                EduSubject twoSubject = twoSubjectList.get(m);
                //判断二级数据哪个一级分类
                if (twoSubject.getParentId().equals(eduSubject.getId())){
                    TwoSubject t = new TwoSubject();
                    BeanUtils.copyProperties(twoSubject,t);
                    twoFinalSubjectList.add(t);
                }
            }
            //
            oneSubject.setChildren(twoFinalSubjectList);
        }

        //封装二级分类
        return finalSubjectList;
    }
}
